/**
 * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.opsli.modulars.ykfire.ykcheck.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.opsli.core.persistence.querybuilder.GenQueryBuilder;
import org.opsli.core.persistence.querybuilder.QueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.opsli.core.base.service.impl.CrudServiceImpl;

import org.opsli.modulars.ykfire.ykcheck.entity.Ykcheck;
import org.opsli.api.wrapper.ykfire.ykcheck.YkcheckModel;
import org.opsli.modulars.ykfire.ykcheck.service.IYkcheckService;
import org.opsli.modulars.ykfire.ykcheck.mapper.YkcheckMapper;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * ykcheck Service Impl
 *
 * @author cy
 * @date 2021-06-04 12:08:26
 */
@Service
public class YkcheckServiceImpl extends CrudServiceImpl<YkcheckMapper, Ykcheck, YkcheckModel>
    implements IYkcheckService {

    @Autowired(required = false)
    private YkcheckMapper mapper;

    @Autowired
    IYkcheckService iYkcheckService;

    @Override
    public Integer countmonth(Integer lastmonth,Integer correct,String areaid){



        Calendar c=Calendar.getInstance();
        c.add(Calendar.MONTH, -lastmonth);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        String gtimelast = sdf.format(c.getTime()); //上月
        System.out.println(gtimelast);
        int lastMonthMaxDay=c.getActualMaximum(Calendar.DAY_OF_MONTH);
        System.out.println(lastMonthMaxDay);
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), lastMonthMaxDay, 23, 59, 59);

        //按格式输出
        String gtime = sdf.format(c.getTime()); //上月最后一天
        System.out.println("上个月最后一天=================="+gtime);
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-01  00:00:00");
        String gtime2 = sdf2.format(c.getTime()); //上月第一天
        System.out.println("上个月第一天============"+gtime2);


        QueryBuilder<Ykcheck> queryBuilder = new GenQueryBuilder<>();
        QueryWrapper<Ykcheck> queryWrapper = queryBuilder.build();
        queryWrapper
                .eq("correct",correct)
                .ne("status",0)
                .between("time",gtime2,gtime)
                .eq("area_id",areaid);
        Integer count = iYkcheckService.count(queryWrapper);
        System.out.println("个数=========="+count);
        return count;
    }



    @Override
    public Integer counttable(Integer lastmonth,Integer correct,String areaid,Integer type){

        Calendar c=Calendar.getInstance();
        c.add(Calendar.MONTH, -lastmonth);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        String gtimelast = sdf.format(c.getTime()); //上月
        System.out.println(gtimelast);
        int lastMonthMaxDay=c.getActualMaximum(Calendar.DAY_OF_MONTH);
        System.out.println(lastMonthMaxDay);
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), lastMonthMaxDay, 23, 59, 59);

        //按格式输出
        String gtime = sdf.format(c.getTime()); //上月最后一天
        System.out.println("上个月最后一天=================="+gtime);
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-01  00:00:00");
        String gtime2 = sdf2.format(c.getTime()); //上月第一天
        System.out.println("上个月第一天============"+gtime2);

        QueryBuilder<Ykcheck> queryBuilder = new GenQueryBuilder<>();
        QueryWrapper<Ykcheck> queryWrapper = queryBuilder.build();
        queryWrapper
                .eq("correct",correct)
                .ne("status",0)
                .between("time",gtime2,gtime)
                .eq("area_id",areaid)
                .eq("type",type);
        Integer count = iYkcheckService.count(queryWrapper);
        System.out.println("个数=========="+count);
        return count;
    }


}